package com.zut.hospital.service.login;

import com.alibaba.fastjson.JSON;
import com.zut.hospital.dao.diagnose.PatientMapper;
import com.zut.hospital.dao.staff.UserMapper;
import com.zut.hospital.entity.diagnose.Patient;
import com.zut.hospital.entity.pharmacy.Sale_Record;
import com.zut.hospital.entity.staff.User;
import com.zut.hospital.entity.staff.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: hos-test
 * @description:
 * @author: 杨曼珍
 * @create: 2019-07-06 09:39
 **/
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    PatientMapper patientMapper;

    @Override
    public Integer loginJudge(String num, String pwd) {
        //  根据num查出当前用户信息
        UserExample example = new UserExample();
        example.createCriteria().andAdNameEqualTo(num);
        List<User> adminUsers = userMapper.selectByExample(example);
        if (adminUsers.size()!=0){
            String pwdStr = adminUsers.get(0).getAdPassword();
            if (pwd.equals(pwdStr)){
                return 1;
            }
        }
        return 0;
    }
    @Override
    public Integer userPower(String name) {
        UserExample example = new UserExample();
        example.createCriteria().andAdNameEqualTo(name);
        User adminUser = userMapper.selectByExample(example).get(0);
        if (adminUser.getAdPower()==0){
                return 1;
        }else
        return 0;
    }
    @Override
    public String countgua(){
        int countgua=0;
        int countjiu=0;
        int countpai=0;
        Date date=new Date();
        List<Map<String, String>> resaultmap = new ArrayList<>();
        String number=new SimpleDateFormat("yyyy-MM-dd").format(date);
        List<Patient>patientList=patientMapper.selectByTime(number);
        countgua=patientList.size();
        for (Patient patient:patientList){
            if (patient.getPatStatus()==1)
                countpai++;
            if (patient.getPatStatus()==2)
                countjiu++;
        }
        Map<String,String>map;
        map=new HashMap<>();
        map.put("countgua",String.valueOf(countgua));
        map.put("countjiu",String.valueOf(countjiu));
        map.put("countpai",String.valueOf(countpai));
        resaultmap.add(map);

        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 1);
        layuiMap.put("msg", "success");
        layuiMap.put("count", 1);
        layuiMap.put("data", resaultmap);
        return JSON.toJSONString(layuiMap);
    }
    @Override
    public  String tu(Date month)throws ParseException {
        String number=new SimpleDateFormat("yyyy-MM-dd").format(month);
        int today=Integer.parseInt(number.substring(number.length()-2));
        List<Map<String,String>>resaultmap=new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(month);//month 为指定月份任意日期
        int year = cal.get(Calendar.YEAR);
        int m = cal.get(Calendar.MONTH);
//        int dayNumOfMonth = getDaysByYearMonth(year, m);
        cal.set(Calendar.DAY_OF_MONTH, 1);// 从一号开始
        for (int i = 0; i < today; i++,cal.add(Calendar.DATE, 1)) {
            Date d = cal.getTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String df = simpleDateFormat.format(d);
            List<Patient>patientList=patientMapper.selectByTime(df);

            Map<String,String>map=new HashMap<>();
            int pai=0;
            int jiu=0;
            int gua=0;
            gua=patientList.size();
            for (Patient patient:patientList){
                if (patient.getPatStatus()==2)
                    jiu++;
                if (patient.getPatStatus()==1)
                    pai++;
            }
            map.put("date",df.substring(df.length()-5));
            map.put("pai",String.valueOf(pai));
            map.put("gua",String.valueOf(gua));
            map.put("jiu",String.valueOf(jiu));
            resaultmap.add(map);
        }
        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 1);
        layuiMap.put("msg", "success");
        layuiMap.put("count", resaultmap.size());
        layuiMap.put("data", resaultmap);
        return JSON.toJSONString(layuiMap);
    }
}
